home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / tp_asm.exe / lha / TP&ASM.OOP < prev    next >
Text File  |  1989-07-31  |  23KB  |  648 lines

  1.  
  2.   TP&Asm           Integrated Compile-Time Assembler          Version 2.2
  3.  
  4.                 Copyright (c) 1989  Richard W. Prescott
  5.                           All Rights Reserved
  6.  
  7. ═══════ Built-In Assembly Language Support for Turbo Pascal Compilers ═══════
  8.  
  9.  All brand and product names mentioned herein are trademarks or registered
  10.                 trademarks of their respective holders.
  11.  
  12.   ┌─────────────────────────────────────────────────────────────────────┐
  13.   │ This file contains detailed reference information to enable you to  │
  14.   │ make effective use of the TP&Asm assembly environment.  For general │
  15.   │ information on getting started please see the README file.          │
  16.   └─────────────────────────────────────────────────────────────────────┘
  17.  
  18. This file contains information specific to the use of TP&Asm with 
  19. Turbo Pascal 5.5 Objects and Methods.  It is intended to supplement 
  20. the information contained in the files TP&ASM.REF and TP&ASM2.REF.
  21.  
  22.  
  23. Turbo Pascal Version 5.5 extends Turbo Pascal through the addition 
  24. of the Object Oriented Programming (OOP) concepts of "Objects" and 
  25. "Methods".  Briefly, an "Object" is a structured type which extends 
  26. the concept of a Record, while a "Method" is a procedure or function
  27. which is associated with an Object in somewhat the same way as data
  28. components are associated with Records.  It is assumed that the reader 
  29. is familiar with these concepts and has read Chapter 1 of the Version
  30. 5.5 OOP Guide.
  31.  
  32. Please keep in mind the following distinctions:
  33.  
  34.    Objects can be STATIC or DYNAMIC;
  35.      according to whether they are statically defined in a VAR 
  36.      or CONST statement, or dynamically allocated on the Heap;
  37.  
  38.    Methods can be STATIC or VIRTUAL;
  39.      according to whether they are defined "virtual", and
  40.      thereby given an entry in a special structure called 
  41.      the Virtual Method Table (VMT);
  42.  
  43.    Method CALLs can be DIRECT or VIRTUAL;
  44.      according to whether the target address of the call is
  45.      "bound" into a direct call at compile time, or obtained
  46.      from a lookup in the Virtual Method Table at run time.
  47.  
  48. A Virtual method call is actually a standard Far Indirect Call in 
  49. which the DWORD (Pointer) address of the method entry code is found 
  50. in the Virtual Method Table.  It is also possible to make indirect
  51. calls via a standard Pascal Pointer or assembly DWord variable, and
  52. the address to be loaded into the pointer can either be bound at 
  53. compile time or obtained from the VMT mechanism.
  54.  
  55. Note that in both Pascal and assembly language it is possible to 
  56. make DIRECT calls to VIRTUAL methods.
  57.  
  58.  
  59.  
  60. The following topics are described below:
  61.  
  62.   1. Using Assembly Language in Methods
  63.   2. Accessing the Self parameter
  64.   3. Accessing Object data fields
  65.   4. Calling an Object's Methods
  66.   5. TypeOf and SizeOf for an Instance
  67.  
  68.  
  69.  
  70. 1. Using Assembly Language in Methods
  71.  
  72. Version 5.5 permits Methods to be written either entirely in Pascal
  73. or entirely in (External) Assembly language.  TP&Asm provides the
  74. option of writing any portion of a Method in assembly language, 
  75. using the TP&Asm "Assemble" statement.  (A method, procedure, or 
  76. function can have any number of Assemble statements).
  77.  
  78. TP&Asm also provides the "Internal" statement, which can be used to 
  79. write an entire method in assembly using standard External Assembly 
  80. language format.  This section provides examples using the Assemble 
  81. and the Internal statements to implement an entire method in 
  82. assembly language.
  83.  
  84. As a simple example, consider the implementation of an "Area" method 
  85. for the Rect object defined on page 103 of the OOP Guide.  The Rect
  86. data fields are defined to be:
  87.  
  88.                   X1, Y1, X2, Y2: Integer;
  89.  
  90. where (X1,Y1) is the upper left corner and (X2,Y2) is the lower right 
  91. corner.  The Pascal implementation of this method would simply be
  92.  
  93.    Function Rect.Area: Integer;
  94.    BEGIN
  95.      Area := (X2 - X1) * (Y2 - Y1);
  96.    END; 
  97.  
  98. for which the compiler generates the following code:
  99.  
  100.    BEGIN
  101.  
  102.    3F4B:0037 55            PUSH BP           ;Standard Entry Code
  103.    3F4B:0038 89E5          MOV BP,SP
  104.    3F4B:003A B80200        MOV AX,0002
  105.    3F4B:003D 9A4402573F    CALL 3F57:0244
  106.    3F4B:0042 83EC02        SUB SP,+02
  107.    ----
  108.      Area := (X2 - X1) * (Y2 - Y1);
  109.  
  110.    3F4B:0045 C47E06        LES DI,[BP+06]    ;Les Di,Self
  111.    3F4B:0048 26            ES:
  112.    3F4B:0049 8B4506        MOV AX,[DI+06]    :Mov Ax,Es:[Di].Y2
  113.    3F4B:004C C47E06        LES DI,[BP+06]    ;Les Di,Self
  114.    3F4B:004F 26            ES:
  115.    3F4B:0050 2B4502        SUB AX,[DI+02]    ;Sub Ax,Es:[Di].Y1
  116.    3F4B:0053 8BD0          MOV DX,AX
  117.    3F4B:0055 C47E06        LES DI,[BP+06]    ;Les Di,Self
  118.    3F4B:0058 26            ES:
  119.    3F4B:0059 8B4504        MOV AX,[DI+04]    ;Mov Bx,Es:[Di].X2
  120.    3F4B:005C C47E06        LES DI,[BP+06]    ;Les Di,Self
  121.    3F4B:005F 26            ES:
  122.    3F4B:0060 2B05          SUB AX,[DI]       ;Sub Bx,Es:[Di].X1
  123.    3F4B:0062 F7EA          IMUL DX           ;IMul Dx
  124.    3F4B:0064 8946FE        MOV [BP-02],AX    ;Mov Area,Ax
  125.    ----
  126.    END; 
  127.  
  128.    3F4B:0067 8B46FE        MOV AX,[BP-02]    ;Standard Exit Code
  129.    3F4B:006A 89EC          MOV SP,BP
  130.    3F4B:006C 5D            POP BP
  131.    3F4B:006D CA0400        RETF 0004
  132.    ----
  133.  
  134.  
  135. 1.1  Using an Assemble statement
  136.  
  137. The following example illustrates how the "Area" method would be
  138. coded using an Assemble statement.  Note that the fields of Rect 
  139. are accessible by name without any explicit assembly definition
  140. (e.g. STRUC), and that since Es:Di is never altered, it is not 
  141. necessary to reload it before each data access.  Standard entry
  142. and exit code is provided by the compiler.
  143.  
  144.  
  145.    Function Rect.Area: Integer;
  146.    BEGIN            {- Compiler generates standard entry code -}
  147.     Assemble
  148.      Les Di,Self         ;Load Self Pointer
  149.      Mov Ax,Es:[Di].Y2
  150.      Sub Ax,Es:[Di].Y1   ;Compute Y2 - Y1
  151.      Mov Dx,Es:[Di].X2
  152.      Sub Dx,Es:[Di].X1   ;Compute X2 - X1
  153.      IMul Dx             ;Compute product
  154.      Mov Area,Ax         ;put in function result
  155.     End; {Assemble}
  156.    END;             {- Compiler generates standard exit code -}
  157.  
  158.  
  159. 1.2  Using an Internal statement - External Assembly format
  160.  
  161. The following example illustrates how the "Area" method would be
  162. coded using an Internal statement in the full External Assembly 
  163. format.  Note that the fields of Rect are again accessible by 
  164. name without any explicit assembly definition.  As with External
  165. assembly code, entry and exit code must be explicitly provided.
  166.  
  167.  
  168.    Internal RectMethods
  169.  
  170.    CODE    SEGMENT BYTE PUBLIC
  171.  
  172.            ASSUME Cs:CODE
  173.  
  174.            PUBLIC Rect@Area
  175.  
  176.    Rect@Area       PROC    FAR
  177.  
  178.    @Self           EQU     DWORD PTR [bp+6]
  179.  
  180.            Push    Bp             ;Explicit
  181.            Mov     Bp,Sp          ;  Entry Code
  182.  
  183.            Les     Di,@Self       ;Load Self Pointer
  184.            Mov     Ax,Es:[Di].Y2
  185.            Sub     Ax,Es:[Di].Y1  ;Compute Y2 - Y1
  186.            Mov     Dx,Es:[Di].X2
  187.            Sub     Dx,Es:[Di].X1  ;Compute X2 - X1
  188.            IMul    Dx             ;Compute product
  189.  
  190.            Mov     Sp,Bp          ;Explicit     (Function
  191.            Pop     Bp             ;  Exit         Result
  192.            Retf    4              ;    Code      is in Ax)
  193.  
  194.    Rect@Area       ENDP
  195.  
  196.    CODE    ENDS
  197.  
  198.            END
  199.  
  200.  
  201. 1.3  Using an Internal statement - reduced overhead format
  202.  
  203. TP&Asm ignores certain "overhead" statements including "Assume" 
  204. and "Public", and can make its own determination of the proper PROC 
  205. model to use.  The following example illustrates how the "Area" 
  206. method would be coded using an Internal statement with the minimum 
  207. necessary "overhead" statements.  Note that the more readable form
  208. "Rect.Area" is permitted in place of "Rect@Area".
  209.  
  210.  
  211.    Internal RectMethods
  212.    CODE SEGMENT
  213.    Rect.Area PROC
  214.      @Self EQU D[bp+6]
  215.      Push Bp            ;Explicit
  216.      Mov Bp,Sp          ;  Entry Code
  217.      Les Di,@Self       ;Load Self Pointer
  218.      Mov Ax,Es:[Di].Y2
  219.      Sub Ax,Es:[Di].Y1  ;Compute Y2 - Y1
  220.      Mov Dx,Es:[Di].X2
  221.      Sub